<?php
/**
 * This page displays a list of projects that have the tag specified.
 * Found at /tag/([a-zA-Z0-9_]+)
 * 
 * This page can only be accessed using mod_rewrite.
 */

    require_once('../SiteIncludes.GLOBAL.php');
    
    if(EclipseMDE::runningOnMod_Rewrite() === false) {
	// Security Guard, Do Not Let This be accessed by any other means than mod_rewrite
	$gVAR['e'] = '404';
	require_once('error_handler.php');
    }
    
    
    try {
	if(isset($gVAR['alias'])) {
	    $Tag =& $TagStash->get($gVAR['alias']);
	    
		$TagPagination = array();
		$TagPagination['info_line'] = "";
		$TagPagination['SQL_Limit'] = 10;
		/**
		 * Pagination 
		 */
		if(isset($gVAR['page']) === true) {
		    $TagPagination['Paginate'] = true;
		    $TagPagination['Display_page'] = ((int)$gVAR['page']); // Mod_rewrite is [0-9]+ this.
		} else {
		    $TagPagination['Paginate'] = true;
		    $TagPagination['Display_page'] = '1';
		}
		if($TagPagination['Display_page'] < 1) {
		    $TagPagination['Display_page'] = 1;
		}
		$TagPagination['SQL_Offset'] = ($TagPagination['Display_page'] * $TagPagination['SQL_Limit'])-$TagPagination['SQL_Limit'];
		$TagPagination['SQL_UpperLimit'] = $Tag->getNumberOfProjectsVisible()-1;
		$TagPagination['Page_UpperLimit'] = $Tag->getNumberOfProjectsVisible();
		$TagPagination['Page_MaxPageNumber'] = $TagPagination['Page_UpperLimit'] / $TagPagination['SQL_Limit'];
		//$TagPagination['Page_UpperLimit'] = ($TagPagination['Display_page'] * $TagPagination['SQL_Limit']) > $TagPagination['SQL_UpperLimit'] ? $TagPagination['SQL_UpperLimit'] : ($TagPagination['Display_page'] * $TagPagination['SQL_Limit']);
		if((($TagPagination['Display_page'] * $TagPagination['SQL_Limit'])-($TagPagination['SQL_Limit']-1)) > ($TagPagination['Page_UpperLimit'])) {
		    $TagPagination['info_line'] = "Invalid Page";
		    $TagPagination['valid_page'] = false;
		} else {
		    $ten_n = ($TagPagination['Display_page'] * $TagPagination['SQL_Limit']);
		    $ten_n_mins_9 = $ten_n - ($TagPagination['SQL_Limit']-1);
		    if($ten_n > $TagPagination['Page_UpperLimit']) {
			$ten_n = $TagPagination['Page_UpperLimit'];
		    }
		    $TagPagination['info_line'] = $ten_n_mins_9.' to '.$ten_n.' of '.$TagPagination['Page_UpperLimit'];
		    $TagPagination['valid_page'] = true;
		}
		
		/*********************************
		 * 
		 * Main HTML Content Creation
		 * Section: Viewing Line
		 * 
		 ********************************/
		
		$info_line = <<<EOEL
		    <div class="row-fluid">
			<div class="span12" style="text-align: right;">
			    <span style="border-bottom: 1px dotted #989898;">Viewing Projects With Tag: <span style="font-weight: bold;">{$Tag->getName()}</span>. <span style="font-style: italic;">({$TagPagination['info_line']})</span></span>
			</div>
		    </div>
EOEL;
			    
		/**
		* -------------------------------------
		*      List Tools Feed 
		* -------------------------------------
		*/
		$announcements_title = "Projects with `{$Tag->getName()}` Tag";
		$announcements_url = "#";
		$announcements_feed_items = array();
		$rss_html = '';
		if($TagPagination['valid_page'] === true) {
		    $query = $sql_conx->query("SELECT `pid` FROM `tool_project` WHERE `visible`='1' AND `pid` IN (SELECT `pid` FROM `tool_tags` WHERE `tid`='{$Tag->getID()}') ORDER BY `creation_time` DESC LIMIT {$TagPagination['SQL_Offset']}, {$TagPagination['SQL_Limit']};");
		    while($row = $query->fetch_assoc()) {
			$item =& $ProjectStash->stash((int)$row['pid']);
			$date = date("d/m/Y", $item->getCreationTime());
			$title = $item->getName();
			$url = EclipseMDE::aliasLink($item->getAlias());
			$description = $item->getDescription();
			$icon = $item->getIconURL();
			$announcements_feed_items[] = $Templater->makecontentfeeditem($date, $title, $url, $description, $icon);
		    }
		    $project_view_feed = $Templater->maincontent_feed($announcements_title, $announcements_url, $announcements_feed_items, $rss_html);
		} else {
		    
		}
		$TagPagination['Display_page'] = 1;//$TagPagination['Display_page'];
		$TagPagination['Page_MaxPageNumber'] = 7;
		if($TagPagination['Display_page'] <= 0 || $TagPagination['Display_page'] > $TagPagination['Page_MaxPageNumber']) {
		    $links_output = '<a href="/tag/'.$Tag->getName().'/1"> 1 </a> ';
		    if($TagPagination['Page_MaxPageNumber'] > 3) {
			$links_output .= '<a href="/tag/'.$Tag->getName().'/2"> 2 </a> ';
			$links_output .= '<a href="/tag/'.$Tag->getName().'/3"> 3 </a> ';
		    } else {
			if($TagPagination['Page_MaxPageNumber'] > 2) {
			    $links_output .= '<a href="/tag/'.$Tag->getName().'/2"> 2 </a> ';
			    $links_output .= '<a href="/tag/'.$Tag->getName().'/3"> 3 </a> ';
			} elseif($TagPagination['Page_MaxPageNumber'] == 2) {
			    $links_output .= '<a href="/tag/'.$Tag->getName().'/2"> 2 </a> ';
			}
		    }
		} else {
		    $links_output = '';
		}
		$project_view_feed .= <<<PAGEI
		    <div class="row-fluid">
			<div class="span12" style="text-align: center;">
			    {$links_output}
			</div>
		    </div>
PAGEI;
		/*********************************
		 * 
		 * HTML Output to TemplateEngine
		 * 
		 ********************************/
			    /**
			     * Central Content 
			     */
		$TemplateEngine->create_content_html($info_line);
		$TemplateEngine->create_content_html($project_view_feed);
		$TemplateEngine->setHasSidebar(false);
		$TemplateEngine->setCentralContentFullWidth(true);
	    
	} else {
	    throw new InvalidTagException('');
	}
    } catch(InvalidTagException $e) {
	// Error Page for no tag
	$gVAR['e'] = '404';
	require_once('error_handler.php');
    }
    
    $TemplateEngine->render();
?>
